package com.jaj.services.rap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jaj.common.HttpUtils;
import com.jaj.models.RapGroup;
import com.jaj.models.RapProject;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

/**
 * Created by chm on 2018/1/25.
 *
 * @author j aj
 */
public class GroupService {

    /**
     * 删除分组
     * @param sessionId 用户SessionId
     * @param groupId 分组Id
     * @return 结果
     */
    public static boolean deleteGroup(String sessionId,String groupId){
        String url=RapConfig.Url.Group.Delete;
        //拼装参数
        Map<String, String> param = new HashMap<>();
        param.put("id", groupId);
        //拼接Cookie（包含登录信息）
        List<Header> headerList = new ArrayList<>();
        Header header = new BasicHeader("Cookie", "JSESSIONID=" + sessionId);
        headerList.add(header);
        try {
            String httpReturn = HttpUtils.httpClientPost(url, param, headerList);
            JSONObject jsonObject = JSON.parseObject(httpReturn);
            if (jsonObject.getInteger("code") == 200) {
                return true;
            }
            return false;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 创建分组.
     *
     * @param sessionId     用户SessionId
     * @param productlineId 产品线Id
     * @param name          分组名字
     * @return 分组的ID
     */
    public static String createGroup(String sessionId, String productlineId, String name) {
        String url = RapConfig.Url.Group.Create;
        //拼装参数
        Map<String, String> param = new HashMap<>();
        param.put("productlineId", productlineId);
        param.put("name", name);
        //拼接Cookie（包含登录信息）
        List<Header> headerList = new ArrayList<>();
        Header header = new BasicHeader("Cookie", "JSESSIONID=" + sessionId);
        headerList.add(header);
        try {
            String httpReturn = HttpUtils.httpClientPost(url, param, headerList);
            JSONObject jb = JSON.parseObject(httpReturn);
            JSONObject jbGroup = (JSONObject) jb.getJSONArray("groups").get(0);
            return jbGroup.getString("id");
        }
        catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 获取所有的分组
     *
     * @param sessionId     用户SessionId
     * @param productlineId 产品线Id
     * @return 分组列表
     */
    public static List<RapGroup> getAllGroups(String sessionId, String productlineId) {
        List<RapGroup> rapGroupList = new ArrayList<>();
        String url = RapConfig.Url.Group.GetAll + "?productlineId=" + productlineId;
        List<Header> headerList = new ArrayList<>();
        Header header = new BasicHeader("Cookie", "JSESSIONID=" + sessionId);
        headerList.add(header);
        try {
            String result = HttpUtils.httpClientGet(url, headerList);
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject != null) {
                for (Object object : jsonObject.getJSONArray("groups")) {
                    JSONObject jsonGroup = (JSONObject) object;
                    RapGroup rapGroup = new RapGroup();
                    rapGroup.setId(jsonGroup.getString("id"));
                    rapGroup.setName(jsonGroup.getString("name"));
                    JSONArray projectArray = jsonGroup.getJSONArray("projects");
                    List<RapProject> rapProjectList = new ArrayList<>();
                    for (Object projectObject : projectArray) {
                        JSONObject jsonProject = (JSONObject) projectObject;
                        RapProject rapProject = new RapProject();
                        rapProject.setId(jsonProject.getString("id"));
                        rapProject.setName(jsonProject.getString("name"));
                        rapProject.setDesc(jsonProject.getString("desc"));
                        rapProjectList.add(rapProject);
                    }
                    rapGroup.setProjects(rapProjectList);
                    rapGroupList.add(rapGroup);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return rapGroupList;
    }

    public static void main(String[] args) {
        String sessionId = LoginService.rapLogin("", "");
//        String plid= createGroup(sessionId,"83","意见反馈与评论接口列表");
//        System.err.println(plid);
//        List<RapGroup> result = getAllGroups(sessionId, "85");
//        System.err.println(JSON.toJSONString(result));
        boolean blResult=deleteGroup(sessionId,"163");
    }
}
